﻿using System;
using System.Collections.Generic;
using System.Windows;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class WebAdminPage : BaseWindow, IWindow
    {
        public Index _Index { get; set; }
        public WebAdminPage(Window window, Dictionary<string, object> parameters)
        {
            Window = window;
            _Parameters = parameters;
            IsDialogWindow = true;
            _Index = parameters.GetValue<Index>("Index");

            InitSet(GetType().Name);

            initView();

            this.SetWindow();

            initLoad();
        }

        void initView()
        {
            var webView1 = Utility.Common.GetProperty(_View, "WebView1");
            webView1["Url"] = _Parameters.GetStringValue("Url");
        }

        Controls.WebView2 _WebView1 { get; set; }

        void initLoad()
        {
            _WebView1 = (Controls.WebView2)GetControl("WebView1");

            _WebView1.ReceiveMessage = (msg) =>
            {
                string type = msg.GetStringValue("type");
                Dictionary<string, object> data = msg.GetValue<Dictionary<string, object>>("data");
                if (type == "closeDialog") this.Window.Close();
                else if (type == "updateDevice") updateDevice(data);
            };
        }

        void updateDevice(Dictionary<string, object> data)
        {
            Guid deviceId = data.GetValue<Guid>("DeviceId");
            int displayIndex = data.GetValue<int>("DisplayIndex");
            byte isPresident = data.GetValue<byte>("IsPresident");
            _Index.UpdateDevice(deviceId, displayIndex, isPresident);
        }
    }
}
